package com.atguigu.gmall.common.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @author gsy
 * @date 2021年09月12日20:39
 * Description
 */
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    //发消息
    public void sendMessage(String exchange,String rountingKey,Object message){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setMessage(message);
        gmallCorrelationData.setRoutingKey(rountingKey);
        //设置主键id
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //以主键id为K,gmallCorrelationData对象为V存入缓存中
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(gmallCorrelationData));
        rabbitTemplate.convertAndSend(exchange,rountingKey,message,gmallCorrelationData);
    }

    //发送延迟消息 基于死信队列实现
    public void sendDeadLetterMessage(String exchange,String rountingKey,Object message,int delayTime){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setMessage(message);
        gmallCorrelationData.setRoutingKey(rountingKey);
        //是否是延迟消息
        gmallCorrelationData.setDelay(true);
        //延迟时间
        gmallCorrelationData.setDelayTime(delayTime);
        //设置主键id
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //以主键id为K,gmallCorrelationData对象为V存入缓存中
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(gmallCorrelationData));
        rabbitTemplate.convertAndSend(exchange,rountingKey,message,m->{
            //设置消息的过期时间 ttl 毫秒
            m.getMessageProperties().setExpiration(String.valueOf(delayTime*1000));
            return m;
        },gmallCorrelationData);
        System.out.println("发生消息时间:"+new Date());
    }

    //发送延迟消息 基于插件实现
    public void sendDelayedMessage(String exchange,String rountingKey,Object message,int delayTime){
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setMessage(message);
        gmallCorrelationData.setRoutingKey(rountingKey);
        //是否是延迟消息
        gmallCorrelationData.setDelay(true);
        //延迟时间
        gmallCorrelationData.setDelayTime(delayTime);
        //设置主键id
        String uuid = UUID.randomUUID().toString();
        gmallCorrelationData.setId(uuid);
        //以主键id为K,gmallCorrelationData对象为V存入缓存中
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(gmallCorrelationData));
        rabbitTemplate.convertAndSend(exchange,rountingKey,message,m->{
            //设置消息的延迟时间 毫秒
            m.getMessageProperties().setDelay(delayTime*1000);
            return m;
        },gmallCorrelationData);
        System.out.println("发送消息时间:"+new Date());
    }
}
